package com.handydata.antifraud.rule.funcs;

import com.handydata.antifraud.common.constant.AlarmInfoCode;
import com.handydata.antifraud.common.constant.DictInfoCode;
import com.handydata.antifraud.to.RiskInputTO;
import com.handydata.antifraud.to.RiskMiddleTO;
import com.handydata.common.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 计算TOPN城市
 */
@Slf4j
@Component
public class CalcCityInTopN extends CalculateAdapter {

    private static final String cityKey = AlarmInfoCode.REDIS_GENERAL_DICT.getCode() + "_" + DictInfoCode.DICT_TOPN_CITY.getCode() + "_";

    @Autowired
    private RedisUtil redisUtil;

    public void doCalc(RiskInputTO riskInputTO, RiskMiddleTO riskMiddleTO) {
        String gpsCity = riskInputTO.getGpsCity();
        String gpsProvince=riskInputTO.getGpsProvince();
//        String province, city;
        if(StringUtils.isEmpty(gpsCity))
            return;

        if(gpsCity.endsWith("市")){
            gpsCity=gpsCity.substring(0,gpsCity.length()-1);
        }
        if(gpsProvince.endsWith("省")){
            gpsProvince=gpsProvince.substring(0,gpsProvince.length()-1);
        }
        log.info(" start cac CalcCityInTopN province{},city{}",gpsProvince,gpsCity);
        String cityHit = redisUtil.getString(cityKey+gpsProvince+gpsCity);

        int res = convert(cityHit);
        riskMiddleTO.setGspCityInTop(String.valueOf(res));
        log.info(" finish cac CalcCityInTopN  cityHit{}",cityHit);

    }
    private static int convert(String str) {
        if(str==null||str.length()<=0)
            return 0;
        return 1;
    }

}
